﻿using Fiddler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ProgramCat.FiddlerExtension
{
    /// <summary>
    /// JSHook模块 单例
    /// </summary>
    public class JsHookMgr : Singleton<JsHookMgr>
    {
        /// <summary>
        /// 是否开启
        /// </summary>
        public bool open = false;
        /// <summary>
        /// 是否正则匹配URL
        /// </summary>
        public bool urlPathEx = false;
        public string urlPath = "";
        /// <summary>
        /// Hook脚本
        /// </summary>
        public string jsHookCode = "";
        private JsHookMgr() { }

        public override void OnSingletonInit()
        {
            
        }

        /// <summary>
        /// Response 开始时自动调用
        /// </summary>
        /// <param name="oSession"></param>
        public void OnResponse(Session oSession)
        {
            //判断资源类型
            if (!oSession.oResponse.headers.ExistsAndContains("Content-Type", "html"))
                return;
            if (!open)
                return;
            //匹配
            bool ExURL = false;
            string fullUrl = oSession.fullUrl;
            if (urlPath == "")
            {
                ExURL = true;
            }
            else
            {
                if (urlPathEx)
                {
                    ExURL = Regex.IsMatch(fullUrl, urlPath);
                }
                else
                {

                    //oSession.utilSetResponseBody(fullUrl);
                    //return;
                    if (fullUrl[fullUrl.Length - 1] == '/')
                    {
                        fullUrl = fullUrl.Remove(fullUrl.Length - 1);
                    }
                    if (urlPath[urlPath.Length - 1] == '/')
                    {
                        urlPath = urlPath.Remove(urlPath.Length - 1);
                    }
                    ExURL = fullUrl == urlPath;
                }
            }
            if (!ExURL)
                return;
            oSession.utilDecodeResponse();
            string htmlStr = "";
            //智能转码
            var charset = oSession.oResponse.headers["Content-Type"].ToLower();
            htmlStr = Encoding.UTF8.GetString(oSession.responseBodyBytes);
            if (charset.IndexOf("charset=utf-7") != -1)
            {
                htmlStr = Encoding.UTF7.GetString(oSession.responseBodyBytes);
            }
            if (charset.IndexOf("charset=utf-32") != -1)
            {
                htmlStr = Encoding.UTF32.GetString(oSession.responseBodyBytes);
            }
            if (charset.IndexOf("charset=gb") != -1)
            {
                htmlStr = Encoding.Default.GetString(oSession.responseBodyBytes);
            }
            if (charset.IndexOf("charset=gbk") != -1)
            {
                htmlStr = Encoding.Default.GetString(oSession.responseBodyBytes);
            }

            //string jsHookCodeTemp = @"<head><script>" + jsHookCode + "</script>";
            //htmlStr = htmlStr.Replace(@"<head>", jsHookCodeTemp);
            //添加hook脚本到网页原数据
            string jsHookCodeTemp = @"<script>" + jsHookCode + "</script>" + htmlStr;
            //通知FD返回
            oSession.utilSetResponseBody(jsHookCodeTemp);
        }
    }
}
